import java.sql.*;

public class Work1 {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String name = "admin"; // 你的数据库用户名
        String pwd = "admin123"; // 你的数据库密码

        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;


        int uid = 0;
        String uemail=null;
        String sql="select * from user where username=? and password=?";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, name, pwd);
           //查询代码
            prep = conn.prepareStatement(sql);
            prep.setString(1, name);
            prep.setString(2, pwd);

            rs = prep.executeQuery();

            while (rs.next()){
                uid = rs.getInt("id");
                uemail = rs.getString("email");
            }


        } catch (ClassNotFoundException e) {
           e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                prep.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println(uid);
        System.out.println(uemail);
    }
}
